<?php
namespace app\admin\model;

use think\Model;
use tree\Tree;

class FestivalModel extends Model{
    
    public function festivalTableTree($currentIds = 0, $tpl = ''){

        $where = ['is_del' => 0];
        $categories = $this->order("list_order ASC")->where($where)->select()->toArray();
        
        $tree       = new Tree();
        $tree->icon = ['&nbsp;&nbsp;│', '&nbsp;&nbsp;├─', '&nbsp;&nbsp;└─'];
        $tree->nbsp = '&nbsp;&nbsp;';
        
        
        $newCategories = [];
        foreach ($categories as $item) {
            $item['checked'] = in_array($item['id'], $currentIds) ? "checked" : "";
            $item['disabled'] = $item['parent_id'] == 0 ? "style='display:none;'" : "";
            $item['image']     = cmf_get_image_url($item['image']);
            $item['str_action'] = '<a href="' . url("Festival/add", ["parent" => $item['id']]) . '">添加子分类</a>  <a href="' . url("Festival/edit", ["id" => $item['id']]) . '">' . lang('EDIT') . '</a>  <a class="js-ajax-delete" href="' . url("Festival/delete", ["id" => $item['id']]) . '">' . lang('DELETE') . '</a> ';
            array_push($newCategories, $item);
        }
        
        $tree->init($newCategories);
        
        if (empty($tpl)) {
            $tpl = "<tr>
                        <td><input name='list_orders[\$id]' type='text' size='3' value='\$list_order' class='input-order'></td>
                        <td>\$id</td>
                        <td>\$spacer \$name</td>
                        <td><img src='\$image' style='max-width:135px; max-height:135px;'/></td>
                        <td>\$str_action</td>
                    </tr>";
        }
        $treeStr = $tree->getTree(0, $tpl);
        
        return $treeStr;
    }
    
    
    public function festivalTree($selectId = 0, $currentCid = 0){

        $where = ['is_del' => 0];
        if (!empty($currentCid)) {
            $where['id'] = ['neq', $currentCid];
        }
        $categories = $this->order("list_order ASC")->where($where)->select()->toArray();
        
        $tree       = new Tree();
        $tree->icon = ['&nbsp;&nbsp;│', '&nbsp;&nbsp;├─', '&nbsp;&nbsp;└─'];
        $tree->nbsp = '&nbsp;&nbsp;';
        
        $newCategories = [];
        foreach ($categories as $item) {
            $item['selected'] = $selectId == $item['id'] ? "selected" : "";
        
            array_push($newCategories, $item);
        }
        
        $tree->init($newCategories);
        $str     = '<option value=\"{$id}\" {$selected}>{$spacer}{$name}</option>';
        $treeStr = $tree->getTree(0, $str);
        
        return $treeStr;
        
    }
}